package com.aitaoxue.palx.ui.dialog;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.aitaoxue.palx.R;


public class DialogAdapter extends BaseAdapter {

	private CharSequence[] _items;
	private int select;
	private boolean showChk = true;

	public DialogAdapter(CharSequence[] items, int selectIdx) {
		_items = items;
		this.select = selectIdx;
	}

	public DialogAdapter(CharSequence[] items) {
		_items = items;
	}

	@Override
	public int getCount() {
		return _items.length;
	}

	@Override
	public String getItem(int i) {
		return _items[i].toString();
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int i, View convertView, ViewGroup container) {
		DialogHolder holder = null;
		if (convertView == null) {
			holder = new DialogHolder();
			convertView = LayoutInflater.from(container.getContext()).inflate(
					R.layout.list_cell_dialog, null, false);
			holder.titleTv = (TextView) convertView.findViewById(R.id.title_tv);
			holder.divider = convertView.findViewById(R.id.list_divider);
			holder.checkIv = (RadioButton) convertView
					.findViewById(R.id.rb_select);
			convertView.setTag(holder);
		} else {
			holder = (DialogHolder) convertView.getTag();
		}

		holder.titleTv.setText(getItem(i));

		if (i == getCount() - 1) {
			holder.divider.setVisibility(View.GONE);
		} else {
			holder.divider.setVisibility(View.VISIBLE);
		}

		if (isShowChk()) {
			holder.checkIv.setVisibility(View.VISIBLE);
			if (select == i) {
				holder.checkIv.setChecked(true);
			} else {
				holder.checkIv.setChecked(false);
			}
		} else {
			holder.checkIv.setVisibility(View.GONE);
		}
		return convertView;
	}

	public boolean isShowChk() {
		return showChk;
	}

	public void setShowChk(boolean showChk) {
		this.showChk = showChk;
	}

	private class DialogHolder {
		public View divider;
		public TextView titleTv;
		public RadioButton checkIv;
	}

}
